﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Meraz.Core.Commands;
using Meraz.Core.Factories;
using Meraz.Core.Repositories;
using Meraz.Core.Model;
using Meraz.Core.Data;
using Ninject;
using System.IO;

namespace Meraz.Core.Managers
{
    public class UrlManager
    {
        public IUrlRepository UrlRepository { get; set; }
        public DomainManager DomainManager { get; set; }
        [Inject]
        public ICreateUrlRedirectCommand CreateUrlRedirectCommand { get; set; }

        [Inject]
        public IObjectFactory ObjectFactory { get; set; }

        public void LoadRedirects(Stream stream)
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    if (string.IsNullOrWhiteSpace(line))
                        continue;
                    string[] delimiters = new string[] { "\t" };
                    string[] tokens = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                    if (tokens.Length != 2)
                    {
                        Debug.WriteLine(string.Format("Problem with this line: '{0}'", line));
                        continue;
                    }
                    AddRedirect(tokens[0], tokens[1]);
                }
            }
            if (stream != null)
            {
                try
                {
                    stream.Close();
                }
                catch
                {
                    //already closed
                }
            }
        }

        public UrlRedirect AddRedirect(string redirect, string destination)
        {
            ISaveUrlRedirectCommand command = ObjectFactory.GetSaveUrlRedirectCommand(redirect, destination);
            try
            {
                command.Execute();
            }catch(Exception ex){
                Debug.WriteLine("Failed to save");
                if (redirect != null)
                    Debug.WriteLine(string.Format("redirect:'{0}'", redirect));
                else
                    Debug.WriteLine("redirect is null");
                if (destination != null)
                    Debug.WriteLine(string.Format("destination: '{0}'", destination));
                else
                    Debug.WriteLine("destination is null");
                Debug.WriteLine(ex);
                    Debug.WriteLine("redirect is null");
                return null;
            }
            return command.Result;
        }

        public void LoadUrl(string href)
        {
            CreateUrlRedirectCommand.Execute(href);
        }

        public Url Load(string href)
        {
            Url found = UrlRepository.Get(href);
            if (found != null)
                return found;
            
            Url created = new Url();
            UrlRepository.Add(created);
            created.Name = href;
            created.Domain = DomainManager.GetFromLink(href);
            return created;
        }
    }
}
